/**
 * author:Administrator
 * date:2016/3/15
 * desc: output format
 * 格式输出
 */

'use strict';

class FormatOutput {
  constructor(errCode, errMsg, payload) {
    this.errCode = errCode;
    if (errMsg) this.errMsg = errMsg;
    if (payload) this.payload = payload;
  }

  isOk() {
    return this.errCode == 0;
  }

  getErrMsg() {
    return this.errMsg;
  }

  getPayload() {
    return this.payload;
  }

  static fromJson(json) {
    if (typeof json === 'string')
      json = JSON.parse(json);
    return new FormatOutput(json.errCode, json.errMsg, json.payload);
  }

  toJson() {
    let json = {
      errCode: this.errCode,
      errMsg: this.errMsg,
      payload: this.payload
    };
    return Object.freeze(json);
  }
}

module.exports = FormatOutput;
